<?php
namespace app\ask\controller;

use think\Controller;

class Collect extends Controller
{
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->PostModel=model("Post.Post");
        $this->CateModel=model("Post.Category");
        $this->BusinessModel=model("Business.Business");
        $this->CommentModel=model("Post.Comment");
        $this->CollectModel=model("Post.Collect");
        $postid=$this->request->param('postid',0,'trim');
        $busid=$this->request->param('busid',0,'trim');
        $this->business=$this->BusinessModel->find($busid);
        if(!$this->business)
        {
            $this->error('请先登录');
            exit;
        }
        $this->post=$this->PostModel->find($postid);
        if(!$this->post)
        {
            $this->error('帖子不存在');
            exit;
        }
    }

    // 检查收藏状态
    public function check()
    {
        if($this->request->isPost())
        {
            // 组装数据
            $where=[
                'busid'=>$this->business->id,
                'postid'=>$this->post->id,
            ];
            $result=$this->CollectModel->where($where)->find();
            if(!$result)
            {
               $this->error('暂未找到收藏记录');
               exit; 
            }
            else
            {
                $this->success('有收藏记录');
                exit;
            }
        }
    }
    

    // 收藏
    public function add()
    {
        if($this->request->isPost())
        {
            // 组装数据
            $data=[
                'busid'=>$this->business->id,
                'postid'=>$this->post->id,
            ];
            $result=$this->CollectModel->validate('common/Post/Collect')->save($data);
            if($result===FALSE)
            {
                $this->error($this->CommentModel->getError());
                exit;
            }else
            {
                $this->success('收藏成功');
                exit;
            }
        }
    }

    // 取消收藏
    public function del()
    {
        if($this->request->isPost())
        {
            // 删除收藏记录
            $delid=$this->request->param('delid',0,'trim');
            if($delid)
            {
                $collect=$this->CollectModel->get($delid);
                if(!$collect)
                {
                    $this->error('收藏记录不存在');
                    exit;
                }
                $result=$collect->delete();

                if($result===FALSE)
                {
                    $this->error('删除收藏记录失败');
                    exit;
                }else
                {
                    $this->success('删除收藏记录成功');
                    exit;
                }
            }
            // 组装数据
            $where=[
                'busid'=>$this->business->id,
                'postid'=>$this->post->id,
            ];

            // 先找出收藏记录
            $collect=$this->CollectModel->get($where);
            
            if(!$collect)
            {
                $this->error('暂未找到收藏记录');
                exit;
            }

            // 直接删除查询到的收藏记录
            $result=$collect->delete();

            if($result===FALSE)
            {
                $this->error('取消收藏失败');
                exit;
            }else
            {
                $this->success('取消收藏成功');
                exit;
            }
        }
    }

    // 查询收藏
    public function collect()
    {
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            var_dump($busid);
            exit;
            $business=$this->BusinessModel->find($busid);
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }
            $where=[
                'busid'=>$business['id']
            ];

            $collect=$this->CollectModel->where($where)->select();
            $CollectCount=$this->CollectModel->where($where)->count();
          
            $data=[
                'post'=>$collect,
                'CollectCount'=>$CollectCount,
            ];
            if(!$collect)
            {
                $this->error('暂无帖子数据');
                exit;
            }else
            {
                $this->success('有帖子数据',null,$data);
                exit;
            }
        }
    }

    // 删除收藏记录
    public function delete()
    {
        if($this->request->isPost())
        {
            $delid=$this->request->param('delid',0,'trim');
            $collect=$this->CollectModel->get($delid);
            if(!$collect)
            {
                $this->error('收藏记录不存在');
                exit;
            }
            halt($collect);
        }
    }
   
}